package com.remainder.common.core.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.*;

/**
 * 用来判断对象属性元素是否为空
 *
 * @Author ASUS
 * @Date 27/05/2022 15:45
 * @Version 1.0
 */
public class BlankUtil {

    /**
     * 普通对象JSON判空并为null值赋默认值
     *
     * @param obj
     * @return
     */
    public static Object initPropertyDefaultValue(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        Method[] methods = obj.getClass().getDeclaredMethods();
        Map<String, Method> methodNameMethodMap = new HashMap(methods.length);
        for (Method method : methods) {
            methodNameMethodMap.put(method.getName().toLowerCase(), method);
        }
        for (Field field : fields) {
            Class<?> clazz = field.getType();
            String propertyName = field.getName();
            try {
                String firstLetter = propertyName.substring(0, 1).toUpperCase();
                String getter = "get" + firstLetter + propertyName.substring(1);
                Method method0 = obj.getClass().getMethod(getter, new Class[] {});
                Object value = method0.invoke(obj, new Object[] {});
                if (value != null) {
                    continue;
                }
            } catch (Exception ignored) {

            }
            Method method = methodNameMethodMap.get("set" + propertyName.toLowerCase());
            if (method == null) {
                continue;
            }
            try {
                if (isWrapClass(clazz) || isCommonDataType(clazz)) {
                    if (clazz == Boolean.class || clazz.getName().contains("boolean")) {
                        method.invoke(obj, Boolean.TRUE);
                    } else if (clazz == Byte.class || clazz.getName().contains("byte")) {
                        method.invoke(obj, new Byte("0"));
                    } else if (clazz == Short.class || clazz.getName().contains("short")) {
                        method.invoke(obj, new Short("0"));
                    } else if (clazz == Integer.class || clazz.getName().contains("int")) {
                        method.invoke(obj, 0);
                    } else if (clazz == Long.class || clazz.getName().contains("long")) {
                        method.invoke(obj, 0L);
                    } else if (clazz == Float.class || clazz.getName().contains("float")) {
                        method.invoke(obj, 0F);
                    } else if (clazz == Double.class || clazz.getName().contains("double")) {
                        method.invoke(obj, 0D);
                    } else if (clazz == Character.class || clazz.getName().contains("char")) {
                        method.invoke(obj, '0');
                    }
                } else if (BigDecimal.class.isAssignableFrom(clazz)) {
                    method.invoke(obj, new BigDecimal("0"));
                } else if (Date.class.isAssignableFrom(clazz)) {
                    method.invoke(obj, new Date());
                } else if (String.class.isAssignableFrom(clazz)) {
                    method.invoke(obj, "");
                } else if (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz)) {
                    Type[] type = method.getGenericParameterTypes();
                    ParameterizedType pType = (ParameterizedType)type[0];
                    Collection collection = null;
                    if (pType.getRawType().toString().endsWith("List")) {
                        collection = new ArrayList();
                    } else if (pType.getRawType().toString().endsWith("Set")) {
                        collection = new HashSet();
                    }
                    Object objValue = null;
                    try {
                        objValue = Class.forName(pType.getActualTypeArguments()[0].getTypeName()).newInstance();
                    } catch (Exception e) {
                    }
                    if (objValue == null) {
                        continue;
                    }
                    Object childObj = initPropertyDefaultValue(objValue);
                    collection.add(childObj);
                    method.invoke(obj, collection);
                } else {
                    method.invoke(obj, initPropertyDefaultValue(clazz.newInstance()));
                }
            } catch (Exception ignored) {

            }
        }
        return obj;
    }

    public static List<?> initListDefaultValue(List<?> list) {
        for (Object o : list) {
            initPropertyDefaultValue(o);
        }
        return list;
    }

    /**
     * 判断是否是基础数据类型，即 int,double,long等类似格式
     */
    public static boolean isCommonDataType(Class clazz) {
        return clazz.isPrimitive();
    }

    /**
     * 判断是否是基础数据类型的包装类型
     *
     * @param clz
     * @return
     */
    public static boolean isWrapClass(Class clz) {
        try {
            return ((Class)clz.getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) {
            return false;
        }
    }

}
